'use strict';
/**
 * @fileoverview 签到
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var _ = require('lodash');
var checkDaySchema = require('./check/checkDaySchema');
var checkMgr = require('../mgr/checkMgr');
var moment = require('moment');

/**
 * 签到
 */
var checkSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 签到日期id
    check_day_list: [checkDaySchema],

    // 上次签到日期
    pre_check_day: {
        type: Number,
        default: 0
    },

    // 不可签到天数
    deny_check_day: {
        type: Number,
        default: 0
    },

    // 上次刷新的时间
    last_refresh_time: {
        type: Date,
        default: 0
    }
});

checkSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 每月初重置签到信息
 * @return {[type]} [description]
 */
checkSchema.methods.resetCheckInfo = function () {
    this.check_day_list = [];
    this.pre_check_day = 0;
    this.deny_check_day = 0;
    this.last_refresh_time = Date.now();
};

/**
 * 获取上次刷新的时间
 * @return {[type]} [description]
 */
checkSchema.methods.getLastRefreshTime = function () {
    return this.last_refresh_time;
};

/**
 * 获取上次签到日期（第几天）
 * @return {[type]} [description]
 */
checkSchema.methods.getPreCheckDay = function () {
    return this.pre_check_day;
};

/**
 * 获取本月累计签到次数
 * @return {[type]} [description]
 */
checkSchema.methods.getTotalCheckDay = function () {
    return this.check_day_list.length;
};

/**
 * 获取本月累计补签次数
 * @return {[type]} [description]
 */
checkSchema.methods.getTotalRetryCheck = function () {
    var total_retry_check = 0;
    _.each(this.check_day_list, function (check_info) {
        if (check_info.status === 2) {
            total_retry_check++;
        }
    });
    return total_retry_check;
};

/**
 * 获取下一个签到日期(本月第几天)
 * @return {[type]} [description]
 */
checkSchema.methods.getNextCheckDay = function () {
    return this.getTotalCheckDay() + 1;
};

/**
 * 签到列表
 * @param  {[type]}  statue  [description]
 * @return {Boolean}         [description]
 */
checkSchema.methods.addCheck = function (check_status) {
    var check_day = this.getNextCheckDay();
    if (check_status === 1) {
        this.pre_check_day = parseInt(moment().format('D'));
    }

    this.check_day_list.push({
        check_day: check_day,
        status: check_status
    });
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
checkSchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
checkSchema.methods.push = function () {
    this.pushModify();
};

checkSchema.methods.pushModify = function () {
    checkMgr.sendCheckChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send check message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!checkSchema.options.toJSON) {
    checkSchema.options.toJSON = {};
}
/* jshint unused:false */
checkSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.last_refresh_time;
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Check', checkSchema);
